package thu.cs.segroup.agent.behaviour;

import java.util.ArrayList;

import thu.cs.segroup.agent.Runner;
import thu.cs.segroup.entity.TCRequirement;
import thu.cs.segroup.util.Constants;
import jade.core.Agent;
import jade.core.behaviours.OneShotBehaviour;
import jade.lang.acl.ACLMessage;

public class TRunnerExecuteTaskBehav extends OneShotBehaviour {

	public TRunnerExecuteTaskBehav(Agent agent) {
		super(agent);
	}
	
	@Override
	public void action() {
		
		Runner runner = (Runner)myAgent;
		String task_name = runner.getTask().getName();
		if (!task_name.contains("new")) {
			ArrayList<String> des = new ArrayList<String>();
			des.add(((Runner)myAgent).getCoor());
			TCRequirement tcr = new TCRequirement();
			tcr.setOld_task_name(task_name);
			tcr.addProperty("category", new String("2"));
			System.out.println("ExecuteTask LOG: " + myAgent.getLocalName() + " finds " + ((Runner)myAgent).getTask().getName() + " should be replaced");
			myAgent.addBehaviour(new CommonSendObjMsgBehav(tcr, des, Constants.RUNNER_REQUEST_TASK, ACLMessage.REQUEST, myAgent));
		}
		else {
			ArrayList<String> des = new ArrayList<String>();
			// System.out.println("ExecuteTask LOG: " + myAgent.getLocalName() + " executed " + ((Runner)myAgent).getTask().getName());
			des.add(myAgent.getLocalName());
			myAgent.addBehaviour(new CommonSendStrMsgBehav("", des, myAgent, Constants.TASK_DONE_MSG_INT,
					ACLMessage.INFORM));
		}
		// TODO Auto-generated method stub

	}

}
